package com.ruoyi.source.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.source.api.domain.SrcCheckRecord;
import com.ruoyi.source.service.CheckRecordService;
import com.ruoyi.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 货物质检Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/check/record")
public class SrcCheckRecordController extends BaseController {
    @Autowired
    private CheckRecordService srcCheckRecordService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private RemoteUserService remoteUserService;


    /**
     * 查询货物质检列表
     */
    @RequiresPermissions("source:check:list")
    @GetMapping("/list")
    public TableDataInfo list(SrcCheckRecord srcCheckRecord) {
        startPage();
        Long userId = SecurityUtils.getUserId();
         srcCheckRecord.setInspectors(userId);
        List<SrcCheckRecord> list = srcCheckRecordService.selectSrcCheckRecordList(srcCheckRecord);
        return getDataTable(list);
    }

    /**
     * 导出货物质检列表
     */
    @RequiresPermissions("source:check:export")
    @Log(title = "货物质检", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcCheckRecord srcCheckRecord) {
        List<SrcCheckRecord> list = srcCheckRecordService.selectSrcCheckRecordList(srcCheckRecord);
        ExcelUtil<SrcCheckRecord> util = new ExcelUtil<SrcCheckRecord>(SrcCheckRecord.class);
        util.exportExcel(response, list, "货物质检数据");
    }

    /**
     * 获取货物质检详细信息
     */
    @RequiresPermissions("source:check:query")
    @GetMapping(value = "/{checkId}")
    public AjaxResult getInfo(@PathVariable("checkId") Long checkId) {
        SrcCheckRecord byId = srcCheckRecordService.getById(checkId);
        return success(byId);
    }


    /**
     * 新增货物质检
     */
    @RequiresPermissions("source:check:add")
    @Log(title = "货物质检", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SrcCheckRecord srcCheckRecord, HttpServletRequest request) {
        //禁止重复提交
        //查询表里有没有goodsid以及goodstype   有：修改状态为2待审核
        LambdaQueryWrapper<SrcCheckRecord> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SrcCheckRecord> eq = wrapper.eq(SrcCheckRecord::getGoodsId, srcCheckRecord.getGoodsId())
                .eq(SrcCheckRecord::getGoodsType, srcCheckRecord.getGoodsType());
        SrcCheckRecord srcCheckRecord1 = srcCheckRecordService.getOne(eq);
        if (srcCheckRecord1 != null) {
            srcCheckRecord1.setStatus("2");
            int i = srcCheckRecordService.updateSrcCheckRecord(srcCheckRecord1);
            return AjaxResult.success("已重新送检，请等待结果");
        } else {
            int i = srcCheckRecordService.insertSrcCheckRecord(srcCheckRecord);
            return AjaxResult.success("送检成功");
        }
    }




    /**
     * 修改货物质检
     */
    @RequiresPermissions("source:check:edit")
    @Log(title = "货物质检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcCheckRecord srcCheckRecord) {
        return toAjax(srcCheckRecordService.updateSrcCheckRecord(srcCheckRecord));
    }

    /**
     * 删除货物质检
     */
    @RequiresPermissions("source:check:remove")
	@DeleteMapping("/{checkIds}")
    public AjaxResult remove(@PathVariable Long[] checkIds) {
        return toAjax(srcCheckRecordService.removeBatchByIds(Arrays.asList(checkIds)));
    }


    /**
     * 报表
     */
    @RequiresPermissions("source:check:export")
    @PostMapping("countProduct")
    public Map<String,Object> countProduct() {
        HashMap<String, Object> map = new HashMap<>();
        List<SrcCheckRecord> list = srcCheckRecordService.selectCheckRecord();
        map.put("list",list);
        map.put("code",200);
        return map;
    }

    @RequestMapping("/deptUsers")
    public AjaxResult getUserByDepts()
    {
        return  remoteUserService.getUserByDept(205);
    }


}
